package controllers
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class AllRequest
	{
		/**
		 *			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			var request:URLRequest = new URLRequest("http://url/to/rest/server");
			request.method = URLRequestMethod.POST;
			request.data = "dummyData"; // Some data needed, otherwise sent as GET request!
			request.requestHeaders = [new URLRequestHeader("X-HTTP-Method-Override", "DELETE")];
			loader.load(request);
		 */		
		public function AllRequest()
		{

		}
		public function getUrl(url:String,callbackFunc:Function,progressFunc:Function=null):void
		{
			var loader:URLLoader = new URLLoader();
			configureListeners(loader,callbackFunc);
			var request:URLRequest = new URLRequest(url);
			decorateHead(request)
			loader.load(request);
		}
		public function postUrl(url:String,obj:Object,callbackFunc:Function,progressFunc:Function=null):void
		{

//			vars.theCityName = "上海";
			var request:URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.POST;
			decorateHead(request)
			request.data = createVariable(obj);

			var loader:URLLoader = new URLLoader();
//			loader.dataFormat =  URLLoaderDataFormat.VARIABLES;
			configureListeners(loader,callbackFunc);
			loader.load(request);
		}

		public function putUrl(url:String,obj:Object,callbackFunc:Function,progressFunc:Function=null):void
		{
			var request:URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.PUT;
			decorateHead(request)
			request.data = createVariable(obj);
			var loader:URLLoader = new URLLoader();
//			loader.dataFormat =  URLLoaderDataFormat.VARIABLES;
			configureListeners(loader,callbackFunc);
			loader.load(request);
		}
		public function deleteUrl(url:String,obj:Object,callbackFunc:Function,progressFunc:Function=null):void
		{
			var loader:URLLoader = new URLLoader();
			configureListeners(loader,callbackFunc)
			var request:URLRequest = new URLRequest("http://url/to/rest/server");
			request.method = URLRequestMethod.DELETE;
			decorateHead(request)
			request.data = createVariable(obj);
//			loader.dataFormat =  URLLoaderDataFormat.VARIABLES;
			loader.load(request); 
		}

		private function decorateHead(request:URLRequest):void
		{
			if(headers.length)
			{
				request.requestHeaders = headers;
			}
		}
		private var headers:Array=[];
		public function addHeader(name:String,value:String):void
		{
			headers.push(new URLRequestHeader(name,value));
		}
		private function createVariable(obj:Object):*
		{
			if(obj is String)
			{
				return obj/*new URLVariables(String(obj));*/
			}
			var vars:URLVariables = new URLVariables();
			for  (var i:String in obj) 
			{
				vars[i] = obj[i];
			}
			return vars;
		}
		private function configureListeners(dispatcher:IEventDispatcher,callback:Function,progressFunc:Function=null):void {
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

			function completeHandler(event:Event):void {
				var loader:URLLoader = URLLoader(event.target);
//				trace("completeHandler: " + loader.data);
				callback(loader.data);
//				var vars:URLVariables = new URLVariables(loader.data);
			}

			function openHandler(event:Event):void {
//				trace("openHandler: " + event);
			}

			function progressHandler(event:ProgressEvent):void {
//				trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
				if(progressFunc!=null)
				{
					progressFunc(event.bytesLoaded/event.bytesTotal);
				}
			}

			function securityErrorHandler(event:SecurityErrorEvent):void {
//				trace("securityErrorHandler: " + event);
				callback(event);
			}

			function httpStatusHandler(event:HTTPStatusEvent):void {
//				trace("httpStatusHandler: " + event);
//				callback(event);
			}

			function ioErrorHandler(event:IOErrorEvent):void {
//				trace("ioErrorHandler: " + event);
				callback(event);
			}
		}


	}
}

